// DlgInputSignalProperty.cpp : implementation file
//

#include "stdafx.h"
#include "MotionControlSoftware.h"
#include "DlgInputSignalProperty.h"
#include "afxdialogex.h"


// CDlgInputSignalProperty dialog

IMPLEMENT_DYNAMIC(CDlgInputSignalProperty, CDialogEx)

CDlgInputSignalProperty::CDlgInputSignalProperty(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgInputSignalProperty::IDD, pParent)
	, m_bEnablePulse(FALSE)
	, m_iPulseTime(0)
	, m_iPort(0)
	, m_bEnablePosMode(FALSE)
	, m_bEnableInSignalH(FALSE)
{
	m_InputSignalBlock = NULL;
	m_pInputSignal = NULL;
}

CDlgInputSignalProperty::~CDlgInputSignalProperty()
{
}

void CDlgInputSignalProperty::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_EnableInputPulse, m_bEnablePulse);
	DDX_Text(pDX, IDC_EDIT_InputPulseTime, m_iPulseTime);
	DDV_MinMaxInt(pDX, m_iPulseTime, 0, 100000000);
	DDX_Text(pDX, IDC_EDIT_InPortNO, m_iPort);
	DDV_MinMaxInt(pDX, m_iPort, 0, 255);
	DDX_Check(pDX, IDC_CHECK_InEnablePosMode, m_bEnablePosMode);
	DDX_Check(pDX, IDC_CHECK_EnableInH, m_bEnableInSignalH);
}


BEGIN_MESSAGE_MAP(CDlgInputSignalProperty, CDialogEx)
//	ON_BN_CLICKED(IDC_CHECK_EnableInputPulse, &CDlgInputSignalProperty::OnBnClickedCheckEnableinputpulse)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_CHECK_EnableInputPulse, &CDlgInputSignalProperty::OnBnClickedCheckEnableinputpulse)
END_MESSAGE_MAP()


// CDlgInputSignalProperty message handlers


BOOL CDlgInputSignalProperty::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
	if (m_InputSignalBlock != NULL)
	{
		CGraph* pGraph = m_InputSignalBlock->GetHead();
		m_pInputSignal = (CInputSignal*)(pGraph->GetHead());
		if (m_pInputSignal->m_iType == 0)
		{
			m_bEnablePulse = FALSE; 
		}
		else
		{
			m_bEnablePulse = TRUE; 
		}
		GetDlgItem(IDC_EDIT_InputPulseTime)->EnableWindow(m_bEnablePulse);

		m_iPort = m_pInputSignal->m_iPort;
		m_iPulseTime = m_pInputSignal->m_iDelayTime;

		if (m_pInputSignal->m_iPosMode == 0)
		{
			m_bEnablePosMode = FALSE;
		}
		else
		{
			m_bEnablePosMode = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_InEnablePosMode))->SetCheck(m_bEnablePosMode);

		if (m_pInputSignal->m_iInEnableSignalType == 0)
		{
			m_bEnableInSignalH = FALSE;
		}
		else
		{
			m_bEnableInSignalH = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_EnableInH))->SetCheck(m_bEnableInSignalH);
	}

	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

bool CDlgInputSignalProperty::UpdateParam()
{
	if (UpdateData())
	{
		if (m_pInputSignal != NULL)
		{
			m_pInputSignal->m_iDelayTime = m_iPulseTime;
			if (m_bEnablePulse)
			{
				m_pInputSignal->m_iType = 1;
			}
			else
			{
				m_pInputSignal->m_iType = 0;
			}
			m_pInputSignal->m_iPort = m_iPort;

			if (m_bEnablePosMode)
			{
				 m_pInputSignal->m_iPosMode = 1;
			}
			else
			{
				m_pInputSignal->m_iPosMode = 0;
			}
			if (m_bEnableInSignalH)
			{
				m_pInputSignal->m_iInEnableSignalType = 1;
			}
			else
			{
				m_pInputSignal->m_iInEnableSignalType = 0;
			}
			return true;
		}
	}
	return false;
}


void CDlgInputSignalProperty::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialogEx::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here
	if (m_InputSignalBlock != NULL)
	{
		CGraph* pGraph = m_InputSignalBlock->GetHead();
		m_pInputSignal = (CInputSignal*)(pGraph->GetHead());
		if (m_pInputSignal->m_iType == 0)
		{
			m_bEnablePulse = FALSE; 
		}
		else
		{
			m_bEnablePulse = TRUE; 
		}

		m_iPort = m_pInputSignal->m_iPort;
		m_iPulseTime = m_pInputSignal->m_iDelayTime;

		GetDlgItem(IDC_EDIT_InputPulseTime)->EnableWindow(m_bEnablePulse);

		if (m_pInputSignal->m_iPosMode == 0)
		{
			m_bEnablePosMode = FALSE;
		}
		else
		{
			m_bEnablePosMode = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_InEnablePosMode))->SetCheck(m_bEnablePosMode);
		if (m_pInputSignal->m_iInEnableSignalType == 0)
		{
			m_bEnableInSignalH = FALSE;
		}
		else
		{
			m_bEnableInSignalH = TRUE;
		}
		((CButton*)GetDlgItem(IDC_CHECK_EnableInH))->SetCheck(m_bEnableInSignalH);
	}

	UpdateData(FALSE);
}


void CDlgInputSignalProperty::OnBnClickedCheckEnableinputpulse()
{
	// TODO: Add your control notification handler code here
	if (UpdateData())
	{
		GetDlgItem(IDC_EDIT_InputPulseTime)->EnableWindow(m_bEnablePulse);
	}
}
